﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fierce.Utility.Helper;

namespace Fierce.Core.Aop
{
    /// <summary>
    /// 服务结果
    /// </summary>
    public class ServiceResult
    {
        /// <summary>
        /// 结果代码
        /// </summary>
        public ServiceResultCode Code { get; set; }

        /// <summary>
        /// 文本消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 数据对象
        /// </summary>
        public object Data { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ServiceResult() { }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="code">结果代码</param>
        /// <param name="message">文本消息</param>
        /// <param name="data">数据对象</param>
        public ServiceResult(ServiceResultCode code, string message = null, object data = null)
        {
            this.Code = code; this.Message = message; this.Data = data;
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <returns>序列化对象字符串</returns>
        public string Serialize()
        {
            return JsonHelper.Serialize(new { code = Code.ToString(), success = Code.Equals(ServiceResultCode.Success), message = Message, data = Data });
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="code">结果代码</param>
        /// <param name="message">文本消息</param>
        /// <param name="data">数据对象</param>
        /// <returns>序列化对象字符串</returns>
        public static string Serialize(ServiceResultCode code, string message = null, object data = null)
        {
            return new ServiceResult(code, message, data).Serialize();
        }
    }
}